package top.fengyulou.xiaofeng.controller.admin;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import top.fengyulou.xiaofeng.common.Jr;
import top.fengyulou.xiaofeng.common.UserUtils;
import top.fengyulou.xiaofeng.domain.Label;
import top.fengyulou.xiaofeng.dto.ClassifyDto;
import top.fengyulou.xiaofeng.dto.LabelDto;
import top.fengyulou.xiaofeng.dto.LabelDto;
import top.fengyulou.xiaofeng.dto.QuestionDto;
import top.fengyulou.xiaofeng.service.LabelService;
import top.fengyulou.xiaofeng.service.UserService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 标签管理
 *
 * @author guochao
 * @version 1.0
 * @date 2022/2/1 11:49
 */
@Controller
public class AdminLabelController {

    @Resource
    private LabelService labelService;

    /**
     * 列表页面
     */
    @RequestMapping("/admin/label/list")
    public String list() {
        return "admin/label/admin-label-list";
    }

    /**
     * 试卷列表
     */
    @RequestMapping("/admin/label/ajax/list")
    @ResponseBody
    public Jr ajaxList(@RequestBody LabelDto labelDto) {
        labelService.listPage(labelDto);
        return Jr.yesd(labelDto);
    }

    /**
     * 试卷列表
     */
    @RequestMapping("/admin/label/ajax/tree")
    @ResponseBody
    public Jr ajaxTree(@RequestParam String paperId) {
        LabelDto query = new LabelDto();
        query.setPaperId(paperId);
        List<LabelDto> list = labelService.listTree(query);
        return Jr.yesd(list);
    }

    //编辑页面
    @RequestMapping("/admin/label/save")
    public String save(String id, Model model) {
        if (StringUtils.isNotBlank(id)) {
            LabelDto label = labelService.getById(id);
            model.addAttribute("data", label);
        }
        return "admin/label/admin-label-save";
    }

    /**
     * 保存
     */
    @RequestMapping("/admin/label/ajax/save")
    @ResponseBody
    public Jr ajaxSave(HttpServletRequest request, LabelDto labelDto) {
        labelDto.setCurrentUserId(UserUtils.getUserId(request));
        labelService.save(labelDto);
        return Jr.yesd(labelDto);
    }

    /**
     * 删除
     */
    @RequestMapping("/admin/label/ajax/delete")
    @ResponseBody
    public Jr ajaxDelete(@RequestParam String[] ids) {
        labelService.delete(ids);
        return Jr.yes();
    }
}
